import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:yp/Xwidgets/line.dart';
import 'package:yp/config/index.dart';
import '../../helper.dart';
import 'logic.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'state.dart';

class MySubscriptionPage extends StatefulWidget {
  MySubscriptionPage({Key key}) : super(key: key);

  @override
  _MySubscriptionPageState createState() {
    return _MySubscriptionPageState();
  }
}

class _MySubscriptionPageState extends State<MySubscriptionPage> {
  final MySubscriptionState state = Get.find<MySubscriptionController>().state;
  final MySubscriptionController logic = Get.put(MySubscriptionController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff5f5f5),
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Color(0xffEA5504),
        elevation: 0,
        title: Text('我的订阅'),
      ),
      body: GetBuilder<MySubscriptionController>(
        init: MySubscriptionController(),
        builder: (logic) {
          return Container(
            color: Color(0xfff5f5f5),
            child: Column(
              children: [
                Container(
                  height: 60,
                  padding: EdgeInsets.only(bottom: 12.0),
                  color: Config.mainColor,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      InkWell(
                        onTap: () {
                          logic.tabSelect(context);
                        },
                        child: Container(
                          padding: EdgeInsets.only(right: 38),
                          width: MediaQuery.of(context).size.width / 2,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text(
                                '直播',
                                style: TextStyle(
                                  color: Color(0xffffffff),
                                  fontSize: 16.0,
                                  fontWeight: FontWeight.bold,
                                ),
                                textAlign: TextAlign.right,
                              ),
                              Container(
                                margin: EdgeInsets.only(top: 4),
                                width: 29.0,
                                height: 4,
                                color: state.shoppingSelect
                                    ? Color(0xffffffff)
                                    : Config.mainColor,
                              ),
                            ],
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          logic.tabSelect(context);
                        },
                        child: Container(
                          padding: EdgeInsets.only(left: 38),
                          width: MediaQuery.of(context).size.width / 2,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '内容',
                                style: TextStyle(
                                  color: Color(0xffffffff),
                                  fontSize: 16.0,
                                  fontWeight: FontWeight.bold,
                                ),
                                textAlign: TextAlign.left,
                              ),
                              Container(
                                margin: EdgeInsets.only(top: 4),
                                width: 29.0,
                                height: 4,
                                color: !state.shoppingSelect
                                    ? Color(0xffffffff)
                                    : Config.mainColor,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                state.shoppingSelect
                    ? _noMsg()
                    : Expanded(
                        child:
                            state.subList.length > 0 ? _content() : Container(),
                      ),
                // _noMsg(),
              ],
            ),
          );
        },
      ),
    );
  }

  // 获取订阅内容
  Widget _content() {
    return state.subList.length > 0
        ? Container(
            padding: EdgeInsets.all(20.w),
            width: MediaQuery.of(context).size.width,
            child: ListView.builder(
              shrinkWrap: true,
              // physics: NeverScrollableScrollPhysics(),
              itemCount: state.subList.length,
              itemBuilder: (context, index) {
                return InkWell(
                  onTap: () {
                    logic.getDetail(context, state.subList[index]);
                  },
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Container(
                        margin: EdgeInsets.only(bottom: 10.0, top: 10.0),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          state.subList[index]['title'],
                          style: TextStyle(
                            fontWeight: FontWeight.w500,
                            fontSize: 20,
                            color: Color(0xff000000),
                          ),
                        ),
                      ),
                      state.subList[index]["img_url"] != ""
                          ? Container(
                              margin: EdgeInsets.only(bottom: 10.0),
                              width: MediaQuery.of(context).size.width,
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(15),
                                child: Image.network(
                                  state.subList[index]["img_url"],
                                  fit: BoxFit.fill,
                                  height: 400.w,
                                ),
                              ),
                            )
                          : Container(),
                      Container(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          state.subList[index]['sub_title'],
                          style: TextStyle(
                            fontWeight: FontWeight.w500,
                            fontSize: 16,
                          ),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(bottom: 10),
                        padding: EdgeInsets.all(6.0),
                        decoration: BoxDecoration(
                          border:
                              Border.all(width: 1, color: Color(0xffcccccc)),
                          borderRadius: BorderRadius.circular(8.0),
                        ),
                        child: InkWell(
                          onTap: () {
                            logic.canleCollectInfo(
                              context,
                              state.subList[index]['series'],
                            );
                          },
                          child: Text('取消订阅'),
                        ),
                      ),
                      YpLine(
                        color: Color(0xffcccccc),
                      ),
                    ],
                  ),
                );
              },
            ),
          )
        : _noMsg();
  }

  Widget _noMsg() {
    return Container(
      color: Color(0xffffffff),
      child: Center(
        child: Column(
          children: [
            SizedBox(
              height: 140.w,
            ),
            Image(
              image: AssetImage("assets/images/nodata.png"),
              width: 218.w,
              height: 164.w,
            ),
            SizedBox(
              height: 20.w,
            ),
            Text(
              '暂无数据',
              style: TextStyle(
                color: Color(0xff999999),
                fontSize: 28.w,
              ),
            ),
            SizedBox(
              height: 40.w,
            ),
          ],
        ),
      ),
    );
  }
}
